package com.permission.controller;

import com.github.pagehelper.PageInfo;
import com.permission.constant.PageConstant;
import com.permission.exception.ParamException;
import com.permission.param.UserParam;
import com.permission.service.SysUserService;
import com.permission.util.ResultVoUtil;
import com.permission.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Administrator on 2018/4/13
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    public ResultVo list(@RequestParam(value = "pageNum", defaultValue = PageConstant.PAGE) Integer pageNum,
                     @RequestParam(value = "pageSize", defaultValue = PageConstant.SIZE) Integer pageSize) {
        PageInfo pageInfo = sysUserService.list(pageNum, pageSize);
        return ResultVoUtil.success(pageInfo);
    }

    @PostMapping
    public ResultVo add(@RequestBody @Valid UserParam userParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ParamException(bindingResult.getFieldError().getDefaultMessage());
        }

        sysUserService.save(userParam);
        return ResultVoUtil.success();
    }
}
